home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / utils / skeleton.el.z / skeleton.el
Encoding:
Text File  |  1998-05-21  |  22.1 KB  |  592 lines

  1. ;;; skeleton.el --- Lisp language extension for writing statement skeletons
  2. ;; Copyright (C) 1993, 1994, 1995 by Free Software Foundation, Inc.
  3.  
  4. ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
  5. ;; Maintainer: FSF
  6. ;; Keywords: extensions, abbrev, languages, tools
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; A very concise language extension for writing structured statement
  30. ;; skeleton insertion commands for programming language modes.  This
  31. ;; originated in shell-script mode and was applied to ada-mode's
  32. ;; commands which shrunk to one third.  And these commands are now
  33. ;; user configurable.
  34.  
  35. ;;; Code:
  36.  
  37. ;; page 1:    statement skeleton language definition & interpreter
  38. ;; page 2:    paired insertion
  39. ;; page 3:    mirror-mode, an example for setting up paired insertion
  40.  
  41.  
  42. (defvar skeleton-transformation nil
  43.   "*If non-nil, function applied to literal strings before they are inserted.
  44. It should take strings and characters and return them transformed, or nil
  45. which means no transformation.
  46. Typical examples might be `upcase' or `capitalize'.")
  47.  
  48. ; this should be a fourth argument to defvar
  49. (put 'skeleton-transformation 'variable-interactive
  50.      "aTransformation function: ")
  51.  
  52.  
  53. (defvar skeleton-autowrap t
  54.   "Controls wrapping behaviour of functions created with `define-skeleton'.
  55. When the region is visible (due to `transient-mark-mode' or marking a region
  56. with the mouse) and this is non-`nil' and the function was called without an
  57. explicit ARG, then the ARG defaults to -1, i.e. wrapping around the visible
  58. region.
  59.  
  60. We will probably delete this variable in a future Emacs version
  61. unless we get a substantial number of complaints about the auto-wrap
  62. feature.")
  63.  
  64. (defvar skeleton-end-hook
  65.   (lambda ()
  66.     (or (eolp) (newline-and-indent)))
  67.   "Hook called at end of skeleton but before going to point of interest.
  68. By default this moves out anything following to next line.
  69. The variables `v1' and `v2' are still set when calling this.")
  70.  
  71.  
  72. ;;;###autoload
  73. (defvar skeleton-filter 'identity
  74.   "Function for transforming a skeleton proxy's aliases' variable value.")
  75.  
  76. (defvar skeleton-untabify t
  77.   "When non-`nil' untabifies when deleting backwards with element -ARG.")
  78.  
  79. (defvar skeleton-newline-indent-rigidly nil
  80.   "When non-`nil', indent rigidly under current line for element `\\n'.
  81. Else use mode's `indent-line-function'.")
  82.  
  83. (defvar skeleton-further-elements ()
  84.   "A buffer-local varlist (see `let') of mode specific skeleton elements.
  85. These variables are bound while interpreting a skeleton.  Their value may
  86. in turn be any valid skeleton element if they are themselves to be used as
  87. skeleton elements.")
  88. (make-variable-buffer-local 'skeleton-further-elements)
  89.  
  90.  
  91. (defvar skeleton-subprompt
  92.   (substitute-command-keys
  93.    "RET, \\<minibuffer-local-map>\\[abort-recursive-edit] or \\[help-command]")
  94.   "*Replacement for %s in prompts of recursive subskeletons.")
  95.  
  96.  
  97. (defvar skeleton-abbrev-cleanup nil
  98.   "Variable used to delete the character that led to abbrev expansion.")
  99.  
  100. ;; XEmacs -- won't byte compile without the wrapper
  101. (eval-and-compile
  102.   (defvar skeleton-debug nil
  103.     "*If non-nil `define-skeleton' will override previous definition."))
  104.  
  105. ;; reduce the number of compiler warnings
  106. (defvar skeleton)
  107. (defvar skeleton-modified)
  108. (defvar skeleton-point)
  109. (defvar skeleton-regions)
  110.  
  111. ;;;###autoload
  112. (defmacro define-skeleton (command documentation &rest skeleton)
  113.   "Define a user-configurable COMMAND that enters a statement skeleton.
  114. DOCUMENTATION is that of the command, while the variable of the same name,
  115. which contains the skeleton, has a documentation to that effect.
  116. INTERACTOR and ELEMENT ... are as defined under `skeleton-insert'."
  117.   (if skeleton-debug
  118.       (set command skeleton))
  119.   `(progn
  120.      (defun ,command (&optional str arg)
  121.        ,(concat documentation
  122.         (if (string-match "\n\\>" documentation)
  123.             "" "\n")
  124.         "\n"
  125.         "This is a skeleton command (see `skeleton-insert').
  126. Normally the skeleton text is inserted at point, with nothing \"inside\".
  127. If there is a highlighted region, the skeleton text is wrapped
  128. around the region text.
  129.  
  130. A prefix argument ARG says to wrap the skeleton around the next ARG words.
  131. A prefix argument of zero says to wrap around zero words---that is, nothing.
  132. This is a way of overiding the use of a highlighted region.")
  133.        (interactive "*P\nP")
  134.        (skeleton-proxy-new ',skeleton str arg))))
  135.  
  136. ;;;###autoload
  137. (defun skeleton-proxy-new (skeleton &optional str arg)
  138.   "Insert skeleton defined by variable of same name (see `skeleton-insert').
  139. Prefix ARG allows wrapping around words or regions (see `skeleton-insert').
  140. If no ARG was given, but the region is visible, ARG defaults to -1 depending
  141. on `skeleton-autowrap'.  An ARG of  M-0  will prevent this just for once.
  142. This command can also be an abbrev expansion (3rd and 4th columns in
  143. \\[edit-abbrevs]  buffer: \"\"  command-name).
  144.  
  145. When called as a function, optional first argument STR may also be a string
  146. which will be the value of `str' whereas the skeleton's interactor is then
  147. ignored."
  148.   (interactive "*P\nP")
  149.   (setq skeleton (funcall skeleton-filter skeleton))
  150.   (if (not skeleton)
  151.       (if (memq this-command '(self-insert-command
  152.                    skeleton-pair-insert-maybe
  153.                    expand-abbrev))
  154.       (setq buffer-undo-list (primitive-undo 1 buffer-undo-list)))
  155.     (skeleton-insert skeleton
  156.              (if (setq skeleton-abbrev-cleanup
  157.                    (or (eq this-command 'self-insert-command)
  158.                    (eq this-command
  159.                        'skeleton-pair-insert-maybe)))
  160.              ()
  161.                ;; Pretend  C-x a e  passed its prefix arg to us
  162.                (if (or arg current-prefix-arg)
  163.                (prefix-numeric-value (or arg
  164.                              current-prefix-arg))
  165.              (and skeleton-autowrap
  166.                   (or (eq last-command 'mouse-drag-region)
  167.                   (and (boundp 'transient-mark-mode)
  168.                        (boundp 'mark-active)
  169.                        transient-mark-mode mark-active))
  170.                   -1)))
  171.              (if (stringp str)
  172.              str))
  173.     (and skeleton-abbrev-cleanup
  174.      (setq skeleton-abbrev-cleanup (point))
  175.      (add-hook 'post-command-hook 'skeleton-abbrev-cleanup nil t))))
  176.  
  177. ;; This command isn't meant to be called, only it's aliases with meaningful
  178. ;; names are.
  179. ;;;###autoload
  180. (defun skeleton-proxy (&optional str arg)
  181.   "Insert skeleton defined by variable of same name (see `skeleton-insert').
  182. Prefix ARG allows wrapping around words or regions (see `skeleton-insert').
  183. If no ARG was given, but the region is visible, ARG defaults to -1 depending
  184. on `skeleton-autowrap'.  An ARG of  M-0  will prevent this just for once.
  185. This command can also be an abbrev expansion (3rd and 4th columns in
  186. \\[edit-abbrevs]  buffer: \"\"  command-name).
  187.  
  188. When called as a function, optional first argument STR may also be a string
  189. which will be the value of `str' whereas the skeleton's interactor is then
  190. ignored."
  191.   (interactive "*P\nP")
  192.   (let ((function (nth 1 (backtrace-frame 1))))
  193.     (if (eq function 'nth)        ; uncompiled Lisp function
  194.     (setq function (nth 1 (backtrace-frame 5)))
  195.       (if (eq function 'byte-code)    ; tracing byte-compiled function
  196.       (setq function (nth 1 (backtrace-frame 2)))))
  197.     (if (not (setq function (funcall skeleton-filter (symbol-value function))))
  198.     (if (memq this-command '(self-insert-command
  199.                  skeleton-pair-insert-maybe
  200.                  expand-abbrev))
  201.         (setq buffer-undo-list (primitive-undo 1 buffer-undo-list)))
  202.       (skeleton-insert function
  203.                (if (setq skeleton-abbrev-cleanup
  204.                  (or (eq this-command 'self-insert-command)
  205.                      (eq this-command
  206.                      'skeleton-pair-insert-maybe)))
  207.                ()
  208.              ;; Pretend  C-x a e  passed its prefix arg to us
  209.              (if (or arg current-prefix-arg)
  210.                  (prefix-numeric-value (or arg
  211.                                current-prefix-arg))
  212.                (and skeleton-autowrap
  213.                 (or (eq last-command 'mouse-drag-region)
  214.                     (and (boundp 'transient-mark-mode)
  215.                      (boundp 'mark-active)
  216.                      transient-mark-mode mark-active))
  217.                 -1)))
  218.                (if (stringp str)
  219.                str))
  220.       (and skeleton-abbrev-cleanup
  221.        (setq skeleton-abbrev-cleanup (point))
  222.        (add-hook 'post-command-hook 'skeleton-abbrev-cleanup nil t)))))
  223.  
  224.  
  225. (defun skeleton-abbrev-cleanup (&rest list)
  226.   "Value for `post-command-hook' to remove char that expanded abbrev."
  227.   (if (integerp skeleton-abbrev-cleanup)
  228.       (progn
  229.     (delete-region skeleton-abbrev-cleanup (point))
  230.     (setq skeleton-abbrev-cleanup nil)
  231.     (remove-hook 'post-command-hook 'skeleton-abbrev-cleanup t))))
  232.  
  233. ;;;###autoload
  234. (defun skeleton-insert (skeleton &optional skeleton-regions str)
  235.   "Insert the complex statement skeleton SKELETON describes very concisely.
  236.  
  237. With optional third REGIONS wrap first interesting point (`_') in skeleton
  238. around next REGIONS words, if REGIONS is positive.  If REGIONS is negative,
  239. wrap REGIONS preceding interregions into first REGIONS interesting positions
  240. \(successive `_'s) in skeleton.  An interregion is the stretch of text between
  241. two contiguous marked points.  If you marked A B C [] (where [] is the cursor)
  242. in alphabetical order, the 3 interregions are simply the last 3 regions.  But
  243. if you marked B A [] C, the interregions are B-A, A-[], []-C.
  244.  
  245. Optional fourth STR is the value for the variable `str' within the skeleton.
  246. When this is non-`nil' the interactor gets ignored, and this should be a valid
  247. skeleton element.
  248.  
  249. SKELETON is made up as (INTERACTOR ELEMENT ...).  INTERACTOR may be nil if
  250. not needed, a prompt-string or an expression for complex read functions.
  251.  
  252. If ELEMENT is a string or a character it gets inserted (see also
  253. `skeleton-transformation').  Other possibilities are:
  254.  
  255.     \\n    go to next line and indent according to mode
  256.     _    interesting point, interregion here, point after termination
  257.     >    indent line (or interregion if > _) according to major mode
  258.     &    do next ELEMENT if previous moved point
  259.     |    do next ELEMENT if previous didn't move point
  260.     -num    delete num preceding characters (see `skeleton-untabify')
  261.     resume:    skipped, continue here if quit is signaled
  262.     nil    skipped
  263.  
  264. Further elements can be defined via `skeleton-further-elements'.  ELEMENT may
  265. itself be a SKELETON with an INTERACTOR.  The user is prompted repeatedly for
  266. different inputs.  The SKELETON is processed as often as the user enters a
  267. non-empty string.  \\[keyboard-quit] terminates skeleton insertion, but
  268. continues after `resume:' and positions at `_' if any.  If INTERACTOR in such
  269. a subskeleton is a prompt-string which contains a \".. %s ..\" it is
  270. formatted with `skeleton-subprompt'.  Such an INTERACTOR may also a list of
  271. strings with the subskeleton being repeated once for each string.
  272.  
  273. Quoted Lisp expressions are evaluated evaluated for their side-effect.
  274. Other Lisp expressions are evaluated and the value treated as above.
  275. Note that expressions may not return `t' since this implies an
  276. endless loop.  Modes can define other symbols by locally setting them
  277. to any valid skeleton element.  The following local variables are
  278. available:
  279.  
  280.     str    first time: read a string according to INTERACTOR
  281.         then: insert previously read string once more
  282.     help    help-form during interaction with the user or `nil'
  283.     input    initial input (string or cons with index) while reading str
  284.     v1, v2    local variables for memorizing anything you want
  285.  
  286. When done with skeleton, but before going back to `_'-point call
  287. `skeleton-end-hook' if that is non-`nil'."
  288.   (and skeleton-regions
  289.        (setq skeleton-regions
  290.          (if (> skeleton-regions 0)
  291.          (list (point-marker)
  292.                (save-excursion (forward-word skeleton-regions)
  293.                        (point-marker)))
  294.            (setq skeleton-regions (- skeleton-regions))
  295.            ;; copy skeleton-regions - 1 elements from `mark-ring'
  296.            (let ((l1 (cons (mark-marker) mark-ring))
  297.              (l2 (list (point-marker))))
  298.          (while (and l1 (> skeleton-regions 0))
  299.            (setq l2 (cons (car l1) l2)
  300.              skeleton-regions (1- skeleton-regions)
  301.              l1 (cdr l1)))
  302.          (sort l2 '<))))
  303.        (goto-char (car skeleton-regions))
  304.        (setq skeleton-regions (cdr skeleton-regions)))
  305.   (let ((beg (point))
  306.     skeleton-modified skeleton-point resume: help input v1 v2)
  307.     (unwind-protect
  308.     (eval `(let ,skeleton-further-elements
  309.          (skeleton-internal-list skeleton str)))
  310.       (run-hooks 'skeleton-end-hook)
  311.       (sit-for 0)
  312.       (or (pos-visible-in-window-p beg)
  313.       (progn
  314.         (goto-char beg)
  315.         (recenter 0)))
  316.       (if skeleton-point
  317.       (goto-char skeleton-point)))))
  318.  
  319. (defun skeleton-read (str &optional initial-input recursive)
  320.   "Function for reading a string from the minibuffer within skeletons.
  321. PROMPT may contain a `%s' which will be replaced by `skeleton-subprompt'.
  322. If non-`nil' second arg INITIAL-INPUT or variable `input' is a string or
  323. cons with index to insert before reading.  If third arg RECURSIVE is non-`nil'
  324. i.e. we are handling the iterator of a subskeleton, returns empty string if
  325. user didn't modify input.
  326. While reading, the value of `minibuffer-help-form' is variable `help' if that
  327. is non-`nil' or a default string."
  328.   (let ((minibuffer-help-form (or (if (boundp 'help) (symbol-value 'help))
  329.                   (if recursive "\
  330. As long as you provide input you will insert another subskeleton.
  331.  
  332. If you enter the empty string, the loop inserting subskeletons is
  333. left, and the current one is removed as far as it has been entered.
  334.  
  335. If you quit, the current subskeleton is removed as far as it has been
  336. entered.  No more of the skeleton will be inserted, except maybe for a
  337. syntactically necessary termination."
  338.                      "\
  339. You are inserting a skeleton.  Standard text gets inserted into the buffer
  340. automatically, and you are prompted to fill in the variable parts.")))
  341.     (eolp (eolp)))
  342.     ;; since Emacs doesn't show main window's cursor, do something noticeable
  343.     (or eolp
  344.     (open-line 1))
  345.     (unwind-protect
  346.     (setq str (if (stringp str)
  347.               (read-string (format str skeleton-subprompt)
  348.                    (setq initial-input
  349.                      (or initial-input
  350.                          (symbol-value 'input))))
  351.             (eval str)))
  352.       (or eolp
  353.       (delete-char 1))))
  354.   (if (and recursive
  355.        (or (null str)
  356.            (string= str "")
  357.            (equal str initial-input)
  358.            (equal str (car-safe initial-input))))
  359.       (signal 'quit t)
  360.     str))
  361.  
  362. (defun skeleton-internal-list (skeleton &optional str recursive)
  363.   (let* ((start (save-excursion (beginning-of-line) (point)))
  364.      (column (current-column))
  365.      (line (buffer-substring start
  366.                  (save-excursion (end-of-line) (point))))
  367.      opoint)
  368.     (or str
  369.     (setq str `(setq str (skeleton-read ',(car skeleton) nil ,recursive))))
  370.     (while (setq skeleton-modified (eq opoint (point))
  371.          opoint (point)
  372.          skeleton (cdr skeleton))
  373.       (condition-case quit
  374.       (skeleton-internal-1 (car skeleton))
  375.     (quit
  376.      (if (eq (cdr quit) 'recursive)
  377.          (setq recursive 'quit
  378.            skeleton (memq 'resume: skeleton))
  379.        ;; remove the subskeleton as far as it has been shown
  380.        ;; the subskeleton shouldn't have deleted outside current line
  381.        (end-of-line)
  382.        (delete-region start (point))
  383.        (insert line)
  384.        (move-to-column column)
  385.        (if (cdr quit)
  386.            (setq skeleton ()
  387.              recursive nil)
  388.          (signal 'quit 'recursive)))))))
  389.   ;; maybe continue loop or go on to next outer resume: section
  390.   (if (eq recursive 'quit)
  391.       (signal 'quit 'recursive)
  392.     recursive))
  393.  
  394.  
  395. (defun skeleton-internal-1 (element &optional literal)
  396.   (cond ((or (integerp element)
  397.          (char-or-string-p element))
  398.      (if (and (integerp element)    ; -num
  399.           (< element 0))
  400.          (if skeleton-untabify
  401.          (backward-delete-char-untabify (- element))
  402.            (delete-backward-char (- element)))
  403.        (insert-before-markers (if (and skeleton-transformation
  404.                        (not literal))
  405.                       (funcall skeleton-transformation element)
  406.                     element))))
  407.     ((eq element '\n)        ; actually (eq '\n 'n)
  408.      (if (and skeleton-regions
  409.           (eq (nth 1 skeleton) '_))
  410.          (progn
  411.            (or (eolp)
  412.            (newline))
  413.            (indent-region (point) (car skeleton-regions) nil))
  414.        (if skeleton-newline-indent-rigidly
  415.            (indent-to (prog1 (current-indentation)
  416.                 (newline)))
  417.          (newline)
  418.          (indent-according-to-mode))))
  419.     ((eq element '>)
  420.      (if (and skeleton-regions
  421.           (eq (nth 1 skeleton) '_))
  422.          (indent-region (point) (car skeleton-regions) nil)
  423.        (indent-according-to-mode)))
  424.     ((eq element '_)
  425.      (if skeleton-regions
  426.          (progn
  427.            (goto-char (car skeleton-regions))
  428.            (setq skeleton-regions (cdr skeleton-regions))
  429.            (and (<= (current-column) (current-indentation))
  430.             (eq (nth 1 skeleton) '\n)
  431.             (end-of-line 0)))
  432.        (or skeleton-point
  433.            (setq skeleton-point (point)))))
  434.     ((eq element '&)
  435.      (if skeleton-modified
  436.          (setq skeleton (cdr skeleton))))
  437.     ((eq element '|)
  438.      (or skeleton-modified
  439.          (setq skeleton (cdr skeleton))))
  440.     ((eq 'quote (car-safe element))
  441.      (eval (nth 1 element)))
  442.     ((or (stringp (car-safe element))
  443.          (consp (car-safe element)))
  444.      (if (symbolp (car-safe (car element)))
  445.          (while (skeleton-internal-list element nil t))
  446.        (setq literal (car element))
  447.        (while literal
  448.          (skeleton-internal-list element (car literal))
  449.          (setq literal (cdr literal)))))
  450.     ((null element))
  451.     ((skeleton-internal-1 (eval element) t))))
  452.  
  453.  
  454. ;; Maybe belongs into simple.el or elsewhere
  455. ;###autoload
  456. (define-skeleton local-variables-section
  457.   "Insert a local variables section.  Use current comment syntax if any."
  458.   (completing-read "Mode: " obarray
  459.            (lambda (symbol)
  460.              (if (commandp symbol)
  461.              (string-match "-mode$" (symbol-name symbol))))
  462.            t)
  463.   '(save-excursion
  464.      (if (re-search-forward page-delimiter nil t)
  465.      (error "Not on last page.")))
  466.   comment-start "Local Variables:" comment-end \n
  467.   comment-start "mode: " str
  468.   & -5 | '(kill-line 0) & -1 | comment-end \n
  469.   ( (completing-read (format "Variable, %s: " skeleton-subprompt)
  470.              obarray
  471.              (lambda (symbol)
  472.                (or (eq symbol 'eval)
  473.                (user-variable-p symbol)))
  474.              t)
  475.     comment-start str ": "
  476.     (read-from-minibuffer "Expression: " nil read-expression-map nil
  477.               'read-expression-history) | _
  478.               comment-end \n)
  479.   resume:
  480.   comment-start "End:" comment-end \n)
  481.  
  482. ;; Variables and command for automatically inserting pairs like () or "".
  483.  
  484. (defvar skeleton-pair nil
  485.   "*If this is nil pairing is turned off, no matter what else is set.
  486. Otherwise modes with `skeleton-pair-insert-maybe' on some keys
  487. will attempt to insert pairs of matching characters.")
  488.  
  489.  
  490. (defvar skeleton-pair-on-word nil
  491.   "*If this is nil, paired insertion is inhibited before or inside a word.")
  492.  
  493.  
  494. (defvar skeleton-pair-filter (lambda ())
  495.   "Attempt paired insertion if this function returns nil, before inserting.
  496. This allows for context-sensitive checking whether pairing is appropriate.")
  497.  
  498.  
  499. (defvar skeleton-pair-alist ()
  500.   "An override alist of pairing partners matched against `last-command-char'.
  501. Each alist element, which looks like (ELEMENT ...), is passed to
  502. `skeleton-insert' with no interactor.  Variable `str' does nothing.
  503.  
  504. Elements might be (?` ?` _ \"''\"), (?\\( ?  _ \" )\") or (?{ \\n > _ \\n ?} >).")
  505.  
  506.  
  507. ;;;###autoload
  508. (defun skeleton-pair-insert-maybe (arg)
  509.   "Insert the character you type ARG times.
  510.  
  511. With no ARG, if `skeleton-pair' is non-nil, pairing can occur.  If the region
  512. is visible the pair is wrapped around it depending on `skeleton-autowrap'.
  513. Else, if `skeleton-pair-on-word' is non-nil or we are not before or inside a
  514. word, and if `skeleton-pair-filter' returns nil, pairing is performed.
  515.  
  516. If a match is found in `skeleton-pair-alist', that is inserted, else
  517. the defaults are used.  These are (), [], {}, <> and `' for the
  518. symmetrical ones, and the same character twice for the others."
  519.   (interactive "*P")
  520.   (let ((mark (and skeleton-autowrap
  521.            (or (eq last-command 'mouse-drag-region)
  522.                (and (boundp 'transient-mark-mode)
  523.                 (boundp 'mark-active)
  524.                 transient-mark-mode mark-active))))
  525.     (skeleton-end-hook))
  526.     (if (or arg
  527.         (not skeleton-pair)
  528.         (and (not mark)
  529.          (or overwrite-mode
  530.              (if (not skeleton-pair-on-word) (looking-at "\\w"))
  531.              (funcall skeleton-pair-filter))))
  532.     (self-insert-command (prefix-numeric-value arg))
  533.       (setq last-command-char (logand last-command-char 255))
  534.       (or skeleton-abbrev-cleanup
  535.       (skeleton-insert
  536.        (cons nil (or (assq last-command-char skeleton-pair-alist)
  537.              (assq last-command-char '((?( _ ?))
  538.                            (?[ _ ?])
  539.                            (?{ _ ?})
  540.                            (?< _ ?>)
  541.                            (?` _ ?')))
  542.              `(,last-command-char _ ,last-command-char)))
  543.        (if mark -1))))))
  544.  
  545.  
  546. ;; A more serious example can be found in sh-script.el
  547. ;;; (defun mirror-mode ()
  548. ;;  "This major mode is an amusing little example of paired insertion.
  549. ;;All printable characters do a paired self insert, while the other commands
  550. ;;work normally."
  551. ;;  (interactive)
  552. ;;  (kill-all-local-variables)
  553. ;;  (make-local-variable 'skeleton-pair)
  554. ;;  (make-local-variable 'skeleton-pair-on-word)
  555. ;;  (make-local-variable 'skeleton-pair-filter)
  556. ;;  (make-local-variable 'skeleton-pair-alist)
  557. ;;  (setq major-mode 'mirror-mode
  558. ;;    mode-name "Mirror"
  559. ;;    skeleton-pair-on-word t
  560. ;;    ;; in the middle column insert one or none if odd window-width
  561. ;;    skeleton-pair-filter (lambda ()
  562. ;;                   (if (>= (current-column)
  563. ;;                       (/ (window-width) 2))
  564. ;;                   ;; insert both on next line
  565. ;;                   (next-line 1)
  566. ;;                 ;; insert one or both?
  567. ;;                 (= (* 2 (1+ (current-column)))
  568. ;;                    (window-width))))
  569. ;;    ;; mirror these the other way round as well
  570. ;;    skeleton-pair-alist '((?) _ ?()
  571. ;;                  (?] _ ?[)
  572. ;;                  (?} _ ?{)
  573. ;;                  (?> _ ?<)
  574. ;;                  (?/ _ ?\\)
  575. ;;                  (?\\ _ ?/)
  576. ;;                  (?` ?` _ "''")
  577. ;;                  (?' ?' _ "``"))
  578. ;;    ;; in this mode we exceptionally ignore the user, else it's no fun
  579. ;;    skeleton-pair t)
  580. ;;  (let ((map (make-vector 256 'skeleton-pair-insert-maybe))
  581. ;;    (i 0))
  582. ;;    (use-local-map `(keymap ,map))
  583. ;;    (while (< i ? )
  584. ;;      (aset map i nil)
  585. ;;      (aset map (+ i 128) nil)
  586. ;;      (setq i (1+ i))))
  587. ;;  (run-hooks 'mirror-mode-hook))
  588.  
  589. (provide 'skeleton)
  590.  
  591. ;; skeleton.el ends here
  592.